package com.kindnesslibrary.activity;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.kindnesslibrary.R;
import com.kindnesslibrary.activity.adapter.XImagePagerAdapter;
import com.kindnesslibrary.base.BaseActivity;
import com.kindnesslibrary.model.XImageModel;
import com.kindnesslibrary.util.Contants;
import com.kindnesslibrary.util.ToastUtil;
import com.kindnesslibrary.ximage.XImage;

import java.util.ArrayList;

/**
 * 图片预览
 */
public class ImagePreviewActivity extends BaseActivity {

    ViewPager vp_image_preview_show;
    XImagePagerAdapter xImagePagerAdapter;
    private View ll_image_preview_title, ll_image_preview_bottom,
            ll_image_preview_select, cv_image_preview_send;
    private ImageView iv_image_preview_select;
    private TextView tv_image_preview_send, tv_image_preview_num;
    private boolean currentSelected = false; // 当前图片是否被选中
    private int currentPosition = 0; // 当前图片所在位置，默认0索引

    @Override
    protected void handlerMessage(Message msg) {

    }

    @Override
    protected int getLayout() {
        return R.layout.activity_image_preview_layout;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        ll_image_preview_title = findViewById(R.id.ll_image_preview_title);
        ll_image_preview_bottom = findViewById(R.id.ll_image_preview_bottom);
        ll_image_preview_select = findViewById(R.id.ll_image_preview_select);
        cv_image_preview_send = findViewById(R.id.cv_image_preview_send);
        tv_image_preview_send = (TextView) findViewById(R.id.tv_image_preview_send);
        tv_image_preview_num = (TextView) findViewById(R.id.tv_image_preview_num);
        iv_image_preview_select = (ImageView) findViewById(R.id.iv_image_preview_select);
        vp_image_preview_show = (ViewPager) findViewById(R.id.vp_image_preview_show);
        vp_image_preview_show.setPageMargin((int) getResources().getDimension(R.dimen.width_2_80));

        // 获取图片集合，由该集合
        ArrayList<String> imageList = new ArrayList<>();
        for (String path : XImage.getConfig().selectImage) {
            imageList.add(path);
        }

        ArrayList<XImageModel> xImageModels = (ArrayList<XImageModel>) getIntent().getSerializableExtra(Contants.ALBUM_LIST);
        xImagePagerAdapter = new XImagePagerAdapter(this, xImageModels, imageList, () -> {
            if (ll_image_preview_title.getVisibility() == View.VISIBLE) {
                ll_image_preview_title.setVisibility(View.GONE);
                ll_image_preview_bottom.setVisibility(View.GONE);
            } else {
                ll_image_preview_title.setVisibility(View.VISIBLE);
                ll_image_preview_bottom.setVisibility(View.VISIBLE);
            }
        });
        vp_image_preview_show.setAdapter(xImagePagerAdapter);

        // 如果选中过图片，则选择图片为被选中图片
        if (XImage.getConfig().selectImage.size() > 0) {
            select();
            tv_image_preview_num.setText("1 / " + XImage.getConfig().selectImage.size());
            tv_image_preview_send.setText("发送(" + XImage.getConfig().selectImage.size() + ")");
        } else {
            tv_image_preview_num.setText("1 / " + xImageModels.size());
        }
    }

    @Override
    protected void initEvent() {
        findViewById(R.id.ll_ximage_preview_back).setOnClickListener(v -> {
            finish();
        });
        tv_image_preview_send.setOnClickListener(v -> {
            ArrayList<String> imageList = xImagePagerAdapter.getImageList();
            ArrayList<XImageModel> xImageModels = xImagePagerAdapter.getxImageModels();
            if(imageList.size() > 0){
                if(XImage.getConfig().selectImage.size() == 0)
                XImage.getConfig().selectImage.add(imageList.get(currentPosition));
                setResult(RESULT_OK);
                finish();
            }
            if(xImageModels.size() > 0){
                if(XImage.getConfig().selectImage.size() == 0)
                XImage.getConfig().selectImage.add(xImageModels.get(currentPosition).filePath);
                setResult(RESULT_OK);
                finish();
            }
        });
        ll_image_preview_select.setOnClickListener(v -> {
            if(XImage.getConfig().selectImage.size() == XImage.getConfig().maxNum && !currentSelected){
                ToastUtil.showToast("最多只能选择" + XImage.getConfig().maxNum + "张哦");
                return;
            }
            // 点击选中图片按钮去选择或取消选择功能
            int position = vp_image_preview_show.getCurrentItem();
            if (xImagePagerAdapter.getImageList().size() > 0) {
                if (XImage.getConfig().selectImage.contains(xImagePagerAdapter.getImageList().get(position))) {
                    XImage.getConfig().selectImage.remove(xImagePagerAdapter.getImageList().get(position));
                    closeSelect();
                } else {
                    XImage.getConfig().selectImage.add(xImagePagerAdapter.getImageList().get(position));
                    select();
                }
            } else {
                if (!XImage.getConfig().selectImage.contains(xImagePagerAdapter.getxImageModels().get(position).filePath)) {
                    XImage.getConfig().selectImage.add(xImagePagerAdapter.getxImageModels().get(position).filePath);
                    select();
                } else {
                    XImage.getConfig().selectImage.remove(xImagePagerAdapter.getxImageModels().get(position).filePath);
                    closeSelect();
                }
            }
            if(XImage.getConfig().selectImage.size() > 0) {
                tv_image_preview_send.setText("发送(" + XImage.getConfig().selectImage.size() + ")");
            }else{
                tv_image_preview_send.setText("发送");
            }
            xImagePagerAdapter.notifyDataSetChanged();
        });
        // 监听viewpager滑动，当page被选中则判断该图片是否被选中，如果选中则图片变成选中状态，如果未选中则图片变成未选中状态
        vp_image_preview_show.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentPosition = position;
                ArrayList<String> imageList = xImagePagerAdapter.getImageList();
                ArrayList<XImageModel> xImageModels = xImagePagerAdapter.getxImageModels();
                String imgPath = "";
                if (imageList.size() > 0) {
                    imgPath = imageList.get(position);
                } else {
                    imgPath = xImageModels.get(position).filePath;
                }
                if (imageList.size() > 0) {
                    if (XImage.getConfig().selectImage.contains(imgPath)) {
                        select();
                    } else {
                        closeSelect();
                    }
                    tv_image_preview_num.setText(position + 1 + " / " + imageList.size());
                } else {
                    if (XImage.getConfig().selectImage.contains(xImageModels.get(position).filePath)) {
                        select();
                    } else {
                        closeSelect();
                    }
                    tv_image_preview_num.setText(position + 1 + " / " + xImageModels.size());
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 被选中
     */
    void select() {
        iv_image_preview_select.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.ximage_check));
        currentSelected = true;
    }

    /**
     * 取消被选中
     */
    void closeSelect() {
        iv_image_preview_select.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.ximage_preview_no));
        currentSelected = false;
    }

    @Override
    protected View getStatusBarView() {
        return null;
    }

}
